<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CheckBrowerObject
 *
 * @author jian.li 
 */
class Tools_CheckBrowerObject extends Erra_Fields_Base_Object {

    public static function checkAndPopUp() {
        $brower_list = array(
            'Chrome' => '10', 'MSIE' => '8.0', 'Safari' => '533.16', 'Firefox' => '5', 'Opera' => "11"
  );
        $brower_link = array(
            'Chrome' => 'https://www.google.com/intl/fr/chrome/browser/?hl=fr',
            'MSIE' => 'http://www.internetexplorer.fr/download',
            'Safari' => 'http://support.apple.com/kb/DL1531?viewlocale=fr_FR',
            'Firefox' => 'http://www.mozilla.org/fr/firefox/new/',
            'Opera' => 'http://www.opera.com/fr/computer'
        );
        $result = false;
        foreach ($brower_list as $brower => $version) {
            $pos = strpos($_SERVER['HTTP_USER_AGENT'], $brower);
            if ($pos !== FALSE) {
                $result = array(
                    'Brower' => $brower
                );
                if ($brower == 'Chrome') {
                    $brower_info = substr($_SERVER['HTTP_USER_AGENT'], $pos, strpos($_SERVER['HTTP_USER_AGENT'], " ", $pos) - $pos);
                    $exp = explode('/', $brower_info);
                    $result['Version'] = $exp[1];
                    if (version_compare($exp[1], $version) < 0) {
                        $result['Last_Version'] = $brower_link[$brower];
                    }
                    break;
                } else if ($brower == 'MSIE') {
                    $brower_info = substr($_SERVER['HTTP_USER_AGENT'], $pos, strpos($_SERVER['HTTP_USER_AGENT'], ";", $pos) - $pos);
                    $exp = explode(' ', $brower_info);
                    $result['Version'] = $exp[1];
                    if (version_compare($exp[1], $version) < 0) {
                        $result['Last_Version'] = $brower_link[$brower];
                    }
                    break;
                } else if ($brower == 'Safari') {
                    $brower_info = substr($_SERVER['HTTP_USER_AGENT'], $pos);
                    $exp = explode('/', $brower_info);
                    $result['Version'] = $exp[1];
                    if (version_compare($exp[1], $version) < 0) {
                        $result['Last_Version'] = $brower_link[$brower];
                    }
                    break;
                } else if ($brower == 'Firefox') {
                    $brower_info = substr($_SERVER['HTTP_USER_AGENT'], $pos);
                    $exp = explode('/', $brower_info);
                    $result['Version'] = $exp[1];
                    if (version_compare($exp[1], $version) < 0) {
                        $result['Last_Version'] = $brower_link[$brower];
                    }
                    break;
                } else if ($brower == 'Opera') {
                    $brower_info = substr($_SERVER['HTTP_USER_AGENT'], $pos, strpos($_SERVER['HTTP_USER_AGENT'], " ", $pos) - $pos);
                    $exp = explode('/', $brower_info);
                    $result['Version'] = $exp[1];
                    if (version_compare($exp[1], $version) < 0) {
                        $result['Last_Version'] = $brower_link[$brower];
                    }
                    break;
                }
            }
        }
        $b_info = $result;
        if (isset($b_info['Last_Version'])) {
            return "<script type='text/javascript'>$(function(){
                var new_brower = $('<div style=\"font-size:13px;\" title=\"Mise à jour de navigateur\">Votre navigateur n\'est pas à jour pour profiter de toutes les fonctionnalités de My Simply Agenda.<br/> Nous vous invitons à mettre à jour votre navigateur.</div>');
                new_brower.dialog(
                {
                    resizable:false,
                    height:220,
                    width:280,
                    modal:true, 
                    buttons:{
                    'Mise à jour':function(){
                        window.location.href=\"" . $b_info['Last_Version'] . "\";
                    }
                }
                }
                );
            });
            </script>";
        }
        return "";
    }

}

